package com.teen9g.infoplatformserver.controller;

import com.teen9g.entity.Total;
import com.teen9g.infoplatformserver.service.CommonService;
import com.teen9g.result.Result;
import com.teen9g.top10VO.CanteenShopTop10VO;
import com.teen9g.top10VO.MaimaiChunithmTop10VO;
import com.teen9g.top10VO.PlaygroundTop10VO;
import com.teen9g.top10VO.TeachingBuildingTop10VO;
import com.teen9g.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/common")
@Slf4j
@CrossOrigin
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private CommonService commonService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传:{}", file);
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //截取原始文件名的后缀 从最后一个点开始往后截取 .jpg
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //文件名拼接
        String ObjectName = UUID.randomUUID() + extension;

        try {
            String path = aliOssUtil.upload(file.getBytes(), ObjectName);
            return Result.success(path);
        } catch (IOException e) {
            log.error("文件上传失败，{}", e);
        }
        return Result.error("文件上传失败");
    }

    @GetMapping("/total")
    public Result<Total> getTotal() {
        log.info("获取各项目的总条目数");
        Total total = commonService.getTotal();
        return Result.success(total);
    }

    @GetMapping("/CStop10")
    public Result<List<CanteenShopTop10VO>> getCSTop10() {
        log.info("获取总浏览量前10的店铺");
        List<CanteenShopTop10VO> top10 = commonService.getCSTop10();
        return Result.success(top10);
    }

    @GetMapping("/TBtop10")
    public Result<List<TeachingBuildingTop10VO>> getTBtop10() {
        log.info("获取总浏览量前10的教学楼");
        List<TeachingBuildingTop10VO> top10 = commonService.getTBTop10();
        return Result.success(top10);
    }

    @GetMapping("/PLtop10")
    public Result<List<PlaygroundTop10VO>> getPLtop10() {
        log.info("获取总浏览量前10的运动场");
        List<PlaygroundTop10VO> top10 = commonService.getPLTop10();
        return Result.success(top10);
    }

    @GetMapping("/MCtop10")
    public Result<List<MaimaiChunithmTop10VO>> getMCtop10() {
        log.info("获取总浏览量前10的舞萌中二条目");
        List<MaimaiChunithmTop10VO> top10 = commonService.getMCTop10();
        return Result.success(top10);
    }
}
